package cn.hist.hfp.controller;

import cn.hist.hfp.RoleType;
import cn.hist.hfp.entity.Manager;
import cn.hist.hfp.service.ManagerService;
import cn.hist.hfp.util.Authentication;
import cn.hist.hfp.vo.ManagerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/Manager")
public class ManagerController {

    private final
    ManagerService managerService;

    private final
    Authentication authentication;

    @Autowired
    public ManagerController(ManagerService managerService, Authentication authentication) {
        this.managerService = managerService;
        this.authentication = authentication;
    }

    @PostMapping
    public int create(@RequestBody Manager manager) {
        return managerService.create(manager).getId();
    }

    @GetMapping("/{id}")
    public ManagerVO findOne(@PathVariable int id, @RequestHeader String token) {
        authentication.permissionCheck(token, RoleType.MANAGER);
        return managerService.findOneVO(id);
    }

    @PutMapping
    public ManagerVO update(@RequestBody Manager manager, @RequestHeader String token) {
        authentication.permissionCheck(token, RoleType.MANAGER);
        return managerService.updateVO(manager);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable int id, @RequestHeader String token) {
        authentication.permissionCheck(token, RoleType.MANAGER);
        managerService.delete(id);
    }
}
